package com.arcvideo.acsconnect.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;

/**
 * @author shenmengchao
 * @version 1.0.0
 * @date 2018/7/25
 * @description
 */

public class FitWidthImageView extends android.support.v7.widget.AppCompatImageView {

    private final static String TAG = FitWidthImageView.class.getSimpleName();

    private int mWindowWidth;

    public FitWidthImageView(Context context) {
        super(context);
    }

    public FitWidthImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FitWidthImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setWindowWidth(int windowWidth) {
        this.mWindowWidth = windowWidth;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        float width = getDrawable().getIntrinsicWidth();
        float height = getDrawable().getIntrinsicHeight();
        float measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
        int measuredHeight = (int) (measuredWidth / width * height);
        if (mWindowWidth != 0) {
            measuredWidth = mWindowWidth;
        }
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        String log = "onMeasure "
                + ",width = " + width
                + ",height = " + height
                + ",measuredWidth = " + measuredWidth
                + ",mWindowWidth = " + mWindowWidth
                + ",measuredHeight = " + measuredHeight;
        Log.d(TAG, log);
//        ToastUtil.showToast(log);
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(measuredHeight, MeasureSpec.EXACTLY));
    }
}
